package com.hontech.libratest.activity.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.hontech.libratest.R
import com.hontech.libratest.activity.fragment.sub.DeliverySub
import com.hontech.libratest.activity.view.LogView
import com.hontech.libratest.env.DeliveryProgEvent
import com.hontech.libratest.env.ExceptEvent
import com.hontech.libratest.utils.eventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DeliveryFragment : Fragment() {

    private lateinit var mLogView: LogView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_delivery, null)
        init(view)
        return view
    }

    private fun init(view: View) {
        mLogView = view.findViewById(R.id.id_del_log_view)
        DeliverySub(view)
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onExceptEvent(env: ExceptEvent) {
        mLogView.append("错误码:${env.ec} ${env.msg}")
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onProgEvent(env: DeliveryProgEvent) {
        mLogView.append(env.msg)
    }

    override fun onStart() {
        super.onStart()
        eventBus.register(this)
    }

    override fun onStop() {
        super.onStop()
        eventBus.unregister(this)
    }
}

